#include "Text.h"
#include "DX11TextureManager.h"
#include "ScreenManager.h"

Text::Text()
{
	m_spacing = 0.1f;
	m_spacing /= TheScreenManager::Instance()->GetAspectRatio();
	m_font = "eras";
	m_colour = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
}

Text::Text(const std::string& s)
{
	m_text = s;
	m_spacing = 0.1f;
	m_spacing /= TheScreenManager::Instance()->GetAspectRatio();
}

Text::~Text()
{

}

bool Text::Initialise()
{
	return TheFontManager::Instance()->LoadFont("eras");
}

void Text::Draw()
{
	TheScreenManager::Instance()->EnableBlending();

	float pos = m_text.size() * -0.5f;
	for (int i = 0; i != m_text.size(); i++)
	{
		int t = (int)m_text[i];

		if (m_text[i] == 'O' || m_text[i] == 'o')
		{
			t = (int)'0' - 15;
			//t += 16;
		}
		else
		{
			t += 17;
		}
		//t += 17;
		TheFontManager::Instance()->SetColour(m_font, t, m_colour);
		TheFontManager::Instance()->Translate(m_font, t, D3DXVECTOR3((m_spacing * pos) + m_position.x, m_position.y, m_position.z));
		TheFontManager::Instance()->Draw(m_font, t);
		pos++;
	}

	TheScreenManager::Instance()->DisableBlending();
}

void Text::Update(const std::string& s)
{
	m_text = s;
}

void Text::Translate(const D3DXVECTOR3& pos)
{
	m_position = pos;
}

void Text::SetColour(const D3DXVECTOR4& col)
{
	m_colour = col;
}

void Text::SetFont(const std::string& font)
{
	m_font = font;
}

bool Text::Intersected(const D3DXVECTOR2& pos)
{
	float width2 = (m_text.size() + 1) * m_spacing * 0.5f;
	float width1 = (m_text.size() - 1) * m_spacing * 0.5f;

	float maxX = m_position.x + width1;
	float minX = m_position.x - width2;
	float maxY = m_position.y + 0.05f;
	float minY = m_position.y - 0.05f;

	if (pos.x > minX && pos.x < maxX)
	{
		if (pos.y > minY && pos.y < maxY)
		{
			return true;
		}
	}

	TheObjMeshManager::Instance()->Draw("sphere");

	return false;
}